import { defineStore } from 'pinia'
import { appRoutes } from '@/router/index'

export type IMenuItem = {
  name: string
  title: string
  icon: string
  children?: IMenuItem[]
}

// interface IState {
//   routes: IMenuItem[]
//   isLoading: boolean
// }

export const useAppStore = defineStore('app', {
  state(): any {
    return {
      routes: [...appRoutes],
      isLoading: false
    }
  }
  // actions: {
  //   setMenu() {
  //     this.isLoading = true
  //     const perStr = sessionStorage.getItem('cloud-permissions')
  //     if (perStr) {
  //       const permissions: string[] = perStr ? JSON.parse(perStr) : []
  //       const tempRoute: IMenuItem[] = []
  //       appRoutes.forEach((route) => {
  //         if (route.children) {
  //           const nowRoute: IMenuItem = {
  //             name: route.name as string,
  //             title: route.meta?.title as string,
  //             icon: route.meta?.icon as string,
  //             children: []
  //           }
  //           route.children.forEach((childRoute) => {
  //             if (childRoute.meta) {
  //               const { permission, hidden } = childRoute.meta
  //               if (
  //                 (permission == null ||
  //                   permissions.some((p) => p === permission)) &&
  //                 !hidden
  //               ) {
  //                 nowRoute.children?.push({
  //                   name: childRoute.name as string,
  //                   title: childRoute.meta?.title as string,
  //                   icon: childRoute.meta?.icon as string
  //                 })
  //               }
  //             }
  //           })
  //           if (nowRoute.children && nowRoute.children.length === 1) {
  //             tempRoute.push(...nowRoute.children)
  //           } else if (nowRoute.children && nowRoute.children.length > 1) {
  //             tempRoute.push(nowRoute)
  //           }
  //         }
  //       })
  //       this.routes = tempRoute
  //       this.isLoading = false
  //     }
  //   },
  //   getPermitRouteName() {
  //     if (this.routes.length) {
  //       if (this.routes[0].children && this.routes[0].children.length) {
  //         return this.routes[0].children[0].name
  //       } else {
  //         return this.routes[0].name
  //       }
  //     }
  //     return 'UserInfo'
  //   }
  //   // hasRoutePermission(name: string) {
  //   //   return this.routes.some((r) => {
  //   //     if (r.children && r.children.length) {
  //   //       return r.children.some((c) => c.name === name)
  //   //     } else {
  //   //       return r.name === name
  //   //     }
  //   //   })
  //   // }
  // }
})
